﻿using Microsoft.EntityFrameworkCore;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Xml;

namespace EFDemo
{
    public class EFCoreContext : DbContext
    {
        public DbSet<EFUser> Users { get; set; }
        public DbSet<Product> Products { get; set; }

        public DbSet<ProfessionalContingent> ProfessionalContingents { get; set; }

        //public EFCoreContext(DbContextOptions<EFCoreContext> option):base(option) { }


        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            //add-migration A1
            //update-database

            //entityframeworkcore\add-migration a4
            //entityframeworkcore\update-database

            optionsBuilder.UseMySql("Server=localhost;Port=3306;User Id=root;Password=123456;Database=EFCore", new MySqlServerVersion(new Version(8, 0, 33)));

            var loggerFactory = new LoggerFactory();
            loggerFactory.AddProvider(new EFLoggerProvider());
            optionsBuilder.UseLoggerFactory(loggerFactory);
            //optionsBuilder.EnableSensitiveDataLogging();

            base.OnConfiguring(optionsBuilder);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            var list = XmlEntityHelper.XmlToEntity();
            foreach (var entityType in modelBuilder.Model.GetEntityTypes())
            {
                var entity = list.FirstOrDefault(c => c.Name == entityType.ClrType.Name);
                var bbb = entity?.Comment ?? "";
                modelBuilder.Entity(entityType.ClrType).HasComment(bbb);

                foreach (var property in entityType.ClrType.GetProperties().Where(x => x.IsPubliclyWritable()))
                {
                    var propertyComment = entity?.Column.FirstOrDefault(c => c.Name == property.Name)?.Comment ?? "";
                    modelBuilder.Entity(entityType.ClrType).Property(property.Name).HasComment(propertyComment);
                }
            }



            base.OnModelCreating(modelBuilder);
        }
    }

    public class EFLoggerProvider : ILoggerProvider
    {
        public ILogger CreateLogger(string categoryName)
        {
            return new EFLogger(categoryName);
        }

        public void Dispose()
        {

        }
    }

    public class EFLogger : ILogger
    {
        private readonly string CategoryName;

        public EFLogger(string categoryName)
        {
            CategoryName = categoryName;
        }

        public IDisposable BeginScope<TState>(TState state)
        {
            return null;
        }

        public bool IsEnabled(LogLevel logLevel)
        {
            return true;
        }

        public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
        {
            if (logLevel == LogLevel.Information && eventId.Id == 20101)
            {
                var logContent = formatter(state, exception);
                Console.WriteLine(logContent);
                Console.ResetColor();
            }
        }
    }
}
